package leetcode;

/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q2555.java
 * @Author : WangRuoyu
 * @Date 2023/2/20 16:26
 */
public class Q2555 {
    public int maximizeWin(int[] prizePositions, int k) {
        if ((prizePositions[prizePositions.length - 1] - prizePositions[0]) <= k) {
            return prizePositions.length;
        }
        int res = 0;

        int a = prizePositions[0];

        int p = 0;
        int q = 0;

        while ((q < prizePositions.length) && (prizePositions[q] <= a + k)) {
            q++;
        }
        res = q - p;

        while (q < prizePositions.length) {
            while (prizePositions[p] == prizePositions[p + 1]) {
                p++;
            }
            p++;
            a = prizePositions[p];
            if (prizePositions[prizePositions.length - 1] <= a + k) {
                res = Math.max(prizePositions.length - p, res);
                return res;
            } else {
                while ((q < prizePositions.length) && (prizePositions[q] <= a + k)) {
                    q++;
                }
                res = Math.max(q - p, res);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Q2555 sl = new Q2555();
        int[] prizePositions = {1, 1, 2, 2, 3, 3, 5};
        int k = 2;
        System.out.println(sl.maximizeWin(prizePositions, k));
    }
}